package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.service.NursingProjectService;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.vo.NursingProjectVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 20843
* @description 针对表【nursing_project(护理项目表)】的数据库操作Service实现
* @createDate 2024-05-07 17:16:04
*/
@Service
@RequiredArgsConstructor
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject>
    implements NursingProjectService{

    private final NursingProjectMapper nursingProjectMapper;

    /**
     * 获取项目分页列表
     *
     * @param name 项目名称，可选参数，用于查询时过滤项目名称包含该参数的项目
     * @param status 项目状态，可选参数，用于查询时过滤项目状态为该参数的项目
     * @param pageNum 请求的页码，用于分页查询
     * @param pageSize 每页显示的数量，用于分页查询
     * @return PageResponse<NursingProjectVO> 分页响应对象，包含当前页的项目列表及其分页信息
     */
    public PageResponse<NursingProjectVO> getPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProjectVO> page = nursingProjectMapper.getPage(name,status);
        return PageResponse.of(page, NursingProjectVO.class);
    }

    /**
     * 添加护理项目的方法。
     *
     * @param nursingProjectDto 护理项目数据传输对象，包含添加护理项目的详细信息。
     * @return 返回操作的结果状态，这里固定返回0，表示操作完成。具体地返回值应根据实际业务逻辑进行定义。
     */
    public int addNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.copyProperties(nursingProjectDto, NursingProject.class);
        try{
            return nursingProjectMapper.insertNursingProject(nursingProject);
        }catch (Exception e){
            throw new BaseException(BasicEnum.NURSINGPROJECT_INSERT_FAIL);
        }
    }

    /**
     * 根据项目ID获取护理项目详情
     *
     * @param id 护理项目的唯一标识符
     * @return NursingProjectVO 护理项目详情的视图对象。如果找不到对应的护理项目，返回null。
     */
    public NursingProjectVO getNursingProjectById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.getById(id);
        if (nursingProject == null) {
            // 返回null或者抛出异常
            throw new BaseException(BasicEnum.NURSINGPROJECT_ID_NOT_EXIST);
        }
        return BeanUtil.copyProperties(nursingProject, NursingProjectVO.class);
    }

    /**
     * 修改护理项目
     *
     * @param nursingProjectDto 护理项目传输参数
     * @return int 修改结果
     */
    public int updateNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.copyProperties(nursingProjectDto, NursingProject.class);
        try{
            return nursingProjectMapper.updateNursingProject(nursingProject);
        }catch (Exception e){
            throw new BaseException(BasicEnum.NURSINGPROJECT_UPDATE_FAIL);
        }
    }


    /**
     * 删除护理项目
     *
     * @param id 护理项目ID
     * @return int 删除结果
     */
    public int deleteNursingProject(Long id) {
        try {
            return nursingProjectMapper.deleteById(id);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.NURSINGPROJECT_DELETE_FAIL);
        }
    }

    /**
     * 更新护理项目状态
     *
     * @param id 护理项目ID
     * @param status 护理项目状态
     * @return int 更新结果
     */
    public int updateStatus(Long id, Integer status) {
        return nursingProjectMapper.updateStatus(id, status,status);
    }

    /**
     * 获取所有护理项目列表
     *
     * @return List<NursingProjectVO> 护理项目列表
     */
    public List<NursingProjectVO> getNursingProjectList() {
        return nursingProjectMapper.getNursingProjectList();
    }
}




